//
//  ShoesCouponsSaleOrderDetailView.swift
//  TestDemo
//
//  Created by ayin on 2019/5/10.
//  Copyright © 2019 aYin. All rights reserved.
//

import UIKit

class ShoesCouponsInfoView: UIView {
    //MARK: - 声明属性
    lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var spotCouponsMarkBtn: UIButton = {
        let btn = UIButton.makeButton(font: UIFont.pingfangRegular(size: 10.up), title: "现货券", titleColor: UIColor.white, image: "calendar_ic_tab_alarm_default", bgColor: UIColor.colorWithHexString("232323"))
        return btn
    }()
    
    lazy var speedDeliveryMarkBtn: UIButton = {
        let btn = UIButton.makeButton(font: UIFont.pingfangRegular(size: 10.up), title: "极速发货", titleColor: UIColor.colorWithHexString("232323"), image: "calendar_ic_tab_alarm_default", bgColor: UIColor.colorWithHexString("CCCCCC"))
        return btn
    }()
    
    lazy var goodsImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.backgroundColor = UIColor.red
        return imgView
    }()
    
    lazy var nameLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangSemibold(size: 12.up), text: "", textColor: UIColor.black, numberLine: 1, alignment: .left)
        return label
    }()
    
    lazy var salePriceLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangSemibold(size: 22.up), text: "出售价：¥0", textColor: UIColor.colorWithHexString("232323"), numberLine: 1, alignment: .left)
        return label
    }()
    
    lazy var buyPriceLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 10.up), text: "买入价: ¥0  发售价: ¥0  尺码：", textColor: UIColor.colorWithHexString("646464"), numberLine: 1, alignment: .left)
        return label
    }()
    
    lazy var timeLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 10.up), text: "入手时间：", textColor: UIColor.colorWithHexString("646464"), numberLine: 1, alignment: .left)
        return label
    }()
    
    lazy var cutLineView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var shoesCouponCodeLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 10.up), text: "鞋券码：", textColor: UIColor.colorWithHexString("646464"), numberLine: 1, alignment: .left)
        return label
    }()
    
    lazy var locationNumberLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 10.up), text: "库位号：", textColor: UIColor.colorWithHexString("646464"), numberLine: 1, alignment: .left)
        return label
    }()
    
    lazy var barcodeImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.backgroundColor = UIColor.red
        return imgView
    }()
    
    var outSpace: CGFloat = 15
    var inSpace: CGFloat = 10
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 搭建UI
    func setupUI() {
        self.addSubview(contentView)
        contentView.addSubview(spotCouponsMarkBtn)
        contentView.addSubview(speedDeliveryMarkBtn)
        contentView.addSubview(goodsImgView)
        contentView.addSubview(nameLB)
        contentView.addSubview(salePriceLB)
        contentView.addSubview(buyPriceLB)
        contentView.addSubview(timeLB)
        contentView.addSubview(cutLineView)
        contentView.addSubview(shoesCouponCodeLB)
        contentView.addSubview(locationNumberLB)
        contentView.addSubview(barcodeImgView)
        
        contentView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(outSpace)
            make.right.bottom.equalToSuperview().offset(-outSpace)
        }
        contentView.layer.shadowColor = UIColor(red: 0.84, green: 0.88, blue: 0.93, alpha: 0.75).cgColor
        contentView.layer.shadowOpacity = 1
        contentView.layer.shadowRadius = 4
        contentView.layer.shadowOffset = CGSize(width: 0, height: 2)
        contentView.layer.cornerRadius = 4
        
        spotCouponsMarkBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(inSpace)
            make.height.equalTo(20.up)
        }
        
        speedDeliveryMarkBtn.snp.makeConstraints { (make) in
            make.left.equalTo(spotCouponsMarkBtn.snp.right).offset(inSpace)
            make.top.height.equalTo(spotCouponsMarkBtn)
        }
        
        goodsImgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(inSpace)
            make.top.equalTo(spotCouponsMarkBtn.snp.bottom).offset(inSpace)
            make.width.height.equalTo(70.up)
        }
        
        nameLB.snp.makeConstraints { (make) in
            make.left.equalTo(goodsImgView.snp.right).offset(inSpace)
            make.top.equalTo(goodsImgView)
            make.right.equalToSuperview().offset(-inSpace)
            make.height.equalTo(20.up)
        }
        
        salePriceLB.snp.makeConstraints { (make) in
            make.left.right.equalTo(nameLB)
            make.top.equalTo(nameLB.snp.bottom)
            make.height.equalTo(30.up)
        }
        
        buyPriceLB.snp.makeConstraints { (make) in
            make.left.right.equalTo(salePriceLB)
            make.top.equalTo(salePriceLB.snp.bottom)
            make.height.equalTo(20.up)
        }
        
        timeLB.snp.makeConstraints { (make) in
            make.left.right.equalTo(buyPriceLB)
            make.top.equalTo(buyPriceLB.snp.bottom)
            make.height.equalTo(20.up)
        }
        
        cutLineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(inSpace)
            make.top.equalTo(timeLB.snp.bottom).offset(inSpace)
            make.right.equalToSuperview().offset(-inSpace)
            make.height.equalTo(1)
        }
        cutLineView.width = screenWidth - 2*outSpace - 2*inSpace
        cutLineView.height = 1
        cutLineView.dottedLineView(UIColor.colorWithHexString("CCCCCC"), lineWidth: 1.0, lineDashPattern: [4,3])
        
        shoesCouponCodeLB.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(inSpace)
            make.top.equalTo(cutLineView.snp.bottom).offset(inSpace)
            make.right.equalToSuperview().offset(-60.up-2*inSpace)
            make.height.equalTo(20.up)
        }
        
        locationNumberLB.snp.makeConstraints { (make) in
            make.left.right.equalTo(shoesCouponCodeLB)
            make.top.equalTo(shoesCouponCodeLB.snp.bottom)
            make.height.equalTo(20.up)
            make.bottom.equalToSuperview().offset(-inSpace)
        }
        
        barcodeImgView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-inSpace)
            make.centerY.equalTo(shoesCouponCodeLB.snp.bottom)
            make.width.equalTo(60.up)
            make.height.equalTo(24.up)
        }
    }
    
    //MARK: - 事件
    //刷新数据
    func refreshDatas(_ data:Any) {
//        goodsImgView.eyee_webImage(with: URL(string: data.imgurl), placeholder: nil)
        nameLB.text = "Off-White X Converse 联名Chuck 1970s"
//        salePriceLB.attributedText = String.addAttributeString(prefixString: "出售价：", contentString: "￥2200", prefixFont: UIFont.pingfangRegular(size: 10.all)!, contentFont: UIFont.dinot(size: 22.all)!)
        buyPriceLB.text = "买入价：¥2200  发售价：¥2200  尺码：36 2/3"
        
//        if hasTime {
            timeLB.isHidden = false
            timeLB.text = "入手时间：2019/05/24"
            
            timeLB.snp.updateConstraints { (make) in
                make.height.equalTo(20.up)
            }
//        }else {
//            timeLB.isHidden = true
//
//            timeLB.snp.updateConstraints { (make) in
//                make.height.equalTo(0.up)
//            }
//        }
        
        shoesCouponCodeLB.text = "鞋券码：EG749144-A12345"
        locationNumberLB.text = "库位号：A23456"
//        barcodeImgView.eyee_webImage(with: URL(string: data.imgurl), placeholder: nil)
    }
}

class ShoesCouponsSaleOrderDetailView: UIView {
    //MARK: - 声明属性
    //鞋券信息view
    lazy var shoesCouponInfoView: ShoesCouponsInfoView = {
        let view = ShoesCouponsInfoView()
        return view
    }()
    
    //银行转账费
    var bankPriceTitleLB: UILabel!
    var bankPriceValueLB: UILabel!
    //仓储费
    var storagePriceTitleLB: UILabel!
    var storagePreferenceLB: UILabel!
    var storagePrice1ValueLB: UILabel!
    var storagePrice2ValueLB: UILabel!
    //预计收入
    var expectIncomeTitleLB: UILabel!
    var expectIncomeValueLB: UILabel!
    
    var space: CGFloat = 15
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 搭建UI
    func setupUI() {
        self.backgroundColor = UIColor.yellow
        
        self.addSubview(shoesCouponInfoView)
        shoesCouponInfoView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
        }
        
        var priceY: CGFloat = 10
        for i in 0 ..< 3 {
            let title = ["银行转账费","仓储费","预计收入"][i]
            let titleLB = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 12.up), text: title, textColor: UIColor.colorWithHexString("333333"), numberLine: 1, alignment: .left)
            self.addSubview(titleLB)
            
            let valueLB = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 12.up), text: "", textColor: UIColor.colorWithHexString("333333"), numberLine: 1, alignment: .right)
            self.addSubview(valueLB)
            
            let labelH: CGFloat = (i != 2 ? 35.up : 40.up)
            titleLB.snp.makeConstraints { (make) in
                make.top.equalTo(shoesCouponInfoView.snp.bottom).offset(priceY)
                make.left.equalToSuperview().offset(space)
                make.height.equalTo(labelH)
            }
            
            valueLB.snp.makeConstraints { (make) in
                make.top.bottom.equalTo(titleLB)
                make.right.equalToSuperview().offset(-space)
            }
            
            if i == 0 {
                bankPriceTitleLB = titleLB
                bankPriceValueLB = valueLB
                
            }else if i == 1 {
                storagePriceTitleLB = titleLB
                storagePrice1ValueLB = valueLB
                
                storagePreferenceLB = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 9.up), text: "", textColor: UIColor.colorWithHexString("333333"), numberLine: 1, alignment: .center)
                storagePreferenceLB.cornerRadius(2)
                storagePreferenceLB.backgroundColor = UIColor.colorWithHexString("FFC700")
                self.addSubview(storagePreferenceLB)
                storagePreferenceLB.snp.makeConstraints { (make) in
                    make.left.equalTo(titleLB.snp.right).offset(5)
                    make.width.equalTo(70.up)
                    make.height.equalTo(20.up)
                    make.centerY.equalTo(titleLB)
                }
                
                storagePrice2ValueLB = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 12.up), text: "", textColor: UIColor.colorWithHexString("CCCCCC"), numberLine: 1, alignment: .right)
                self.addSubview(storagePrice2ValueLB)
                storagePrice2ValueLB.snp.makeConstraints { (make) in
                    make.top.height.equalTo(valueLB)
                    make.right.equalTo(valueLB.snp.left).offset(-10)
                }
                
                let cutLineView = UIView()
                cutLineView.backgroundColor = UIColor.colorWithHexString("F5F5F5")
                self.addSubview(cutLineView)
                cutLineView.snp.makeConstraints { (make) in
                    make.left.equalToSuperview().offset(space)
                    make.right.equalToSuperview().offset(-space)
                    make.top.equalTo(titleLB.snp.bottom).offset(-1)
                    make.height.equalTo(1)
                }
            }else {
                expectIncomeTitleLB = titleLB
                expectIncomeValueLB = valueLB
            }
            
            priceY = priceY + 35.up
        }
    }
    
    //MARK: - 事件
    //刷新数据
    func refreshDatas(_ data:Any) {
        shoesCouponInfoView.refreshDatas(data)
        
        bankPriceTitleLB.text = "银行转账费（0.8%）"
        bankPriceValueLB.text = "¥ 12.30"
        
        storagePriceTitleLB.text = "仓储费（0.2元/天)"
        storagePreferenceLB.text = "特惠期0费用"
        storagePrice1ValueLB.text = "¥ 0.00"
        storagePrice2ValueLB.text = "¥ 16.70"
        storagePrice2ValueLB.addDeleteLine(UIColor.colorWithHexString("CCCCCC"))
        
        expectIncomeValueLB.text = "¥ 1850.00"
    }
}
